package com.javasm.duanju.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.duanju.dao.VideoDao;
import com.javasm.duanju.dto.VideoBaseDTO;
import com.javasm.duanju.entity.Video;
import com.javasm.duanju.entity.VideoItem;
import com.javasm.duanju.enums.VideoStatus;
import com.javasm.duanju.service.VideoItemService;
import com.javasm.duanju.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Video)表服务实现类
 *
 * @author makejava
 * @since 2025-02-27 15:31:25
 */
@Service("videoService")
public class VideoServiceImpl extends ServiceImpl<VideoDao, Video> implements VideoService {
    @Autowired
    VideoItemService videoItemService;


//下架功能
    @Override
    public void downVideo(VideoBaseDTO videoBaseDTO) {
//        通过短剧id,准备数据
        Long id = videoBaseDTO.getId();
        Video video = this.getById(id);
        //        修改状态
        video.setStatus(VideoStatus.DOWN.getStatus());
      LambdaQueryWrapper<VideoItem> queryWrapper=new LambdaQueryWrapper<>();
      queryWrapper.eq(VideoItem::getId,id);
//      查询出来为短剧集表
        List<VideoItem> videoItems = videoItemService.list(queryWrapper);
//        修改短剧集的状态，全部下架
        List<VideoItem> newVideoItemList = videoItems.stream().map(videoItem -> {
            videoItem.setStars(VideoStatus.DOWN.getStatus());
            return videoItem;
        }).collect(Collectors.toList());
//           更新正式表为下架状态
         updateById(video);
//         对短剧集进行下架
        videoItemService.updateBatchById(newVideoItemList);
        //TODO 该短剧应该从 推荐列表、剧单 中删除
    }





}

